package com.fstech.whiteboadrd.data;

import android.os.Environment;
import android.util.Log;

import com.fstech.whiteboadrd.R;
import com.fstech.whiteboadrd.control.ControlManager;
import com.fstech.whiteboadrd.lib.bean.SketchData;
import com.fstech.whiteboadrd.lib.view.SketchView;
import com.fstech.whiteboadrd.utils.UIUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DataManager {

    public static final int OPERATE_ADD = 1;
    public static final int OPERATE_DEL = 2;
    public static final int OPERATE_SEL = 3;

    public static int width = 1280;
    public static int height = 800;

    //文件保存目录
    public static final String TEMP_FILE_PATH = Environment.getExternalStorageDirectory().toString() + "/YingHe/temp/";
    public static final String FILE_PATH = Environment.getExternalStorageDirectory().toString() + File.separator + "FuSiTech" + File.separator + "sketchPhoto" + File.separator;
    public static final String TEMP_FILE_NAME = "temp_";

    // 橡皮擦大小
    public static final int ERASE_SIZE_SMALL = 10;
    public static final int ERASE_SIZE_MEDIUM = 30;
    public static final int ERASE_SIZE_BIG = 50;

    //笔大小
    public static final int PEN_SIZE_SMALL = 3;
    public static final int PEN_SIZE_MEDIUM = 9;
    public static final int PEN_SIZE_BIG = 18;

    //笔颜色 默认
    public static final int PEN_COLOR_BLACK = UIUtils.getColor(R.color.black);

    public static final int BG_COLOR_BLACK = UIUtils.getColor(R.color.wb_bg6);

    private static DataManager mInstance;

    private List<SketchData> sketchDataList = new ArrayList<>();

    private int mCurrentIndex;

    private String savePath;

    private int penColor;

    private int penSize;

    private int eraseSize;

    private int bgColor;

    private int bgColorRes;
    private SketchData latestSketchData;

    public DataManager() {
        initHistories();
    }

    public static DataManager getInstance() {
        if (mInstance == null) {
            mInstance = new DataManager();
        }
        return mInstance;
    }

    public void initHistories() {
        sketchDataList.clear();
//        File directory = new File(FILE_PATH);
//        if (directory.exists() && directory.isDirectory()) {
//            File[] files = directory.listFiles();
//            for (File file : files) {
//                if (file.isFile() && file.getName().endsWith(".png")) {
//                    sketchDataList.add(SketchData.newInstance(file.getAbsolutePath()));
//                }
//            }
//        }
    }

    public String getSavePath() {
        return savePath;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public int getPenColor() {
        return penColor;
    }

    public void setPenColor(int penColor) {
        this.penColor = penColor;
    }

    public void setPenColorRes(int penColorRes) {
        setPenColor(UIUtils.getColor(penColorRes));
    }

    public int getPenSize() {
        return penSize;
    }

    public void setPenSize(int penSize) {
        this.penSize = penSize;
    }

    public int getEraseSize() {
        return eraseSize;
    }

    public void setEraseSize(int eraseSize) {
        this.eraseSize = eraseSize;
    }

    public int getBgColor() {
        return bgColor;
    }

    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
    }

    public void addNew(SketchData newSketchData) {
        sketchDataList.add(newSketchData);
    }

    public int getBgColorRes() {
        return bgColorRes;
    }

    public void setBgColorRes(int bgColorRes) {
        this.bgColorRes = bgColorRes;
        bgColor = UIUtils.getColor(bgColorRes);
    }

    public void addSketchData(SketchData sketchData) {
        sketchDataList.add(sketchData);
    }

    public List<SketchData> getDataList() {
        return sketchDataList;
    }

    // 从1开始
    public int getCurrentIndex() {
        return mCurrentIndex;
    }

    public SketchData getCurrentData() {
        return getDataList().get(mCurrentIndex - 1);
    }

    public int getTotalPages() {
        return sketchDataList.size();
    }

    public void setSelected(int position) {
        mCurrentIndex = position + 1;
        operateDataList(OPERATE_SEL, null);
    }

    public void setSelected(SketchData sketchData) {
        operateDataList(OPERATE_SEL, sketchData);
    }

    public SketchData addNewSketchData() {
        Log.e("yangxing", "addNewSketchData");
        SketchData newSketchData = SketchData.newInstance();
        operateDataList(OPERATE_ADD, newSketchData);
        return newSketchData;
    }

    public SketchData removeSketchData(int position) {
        sketchDataList.remove(position);
        if (mCurrentIndex > sketchDataList.size()) {
            mCurrentIndex = sketchDataList.size();
        }
        operateDataList(OPERATE_DEL, null);
        return sketchDataList.get(mCurrentIndex - 1);
    }

    /**
     * @param operateType 增（1） 删（2）
     * @param sketchData
     */
    private void operateDataList(int operateType, SketchData sketchData) {
        switch (operateType) {
            case OPERATE_ADD:
                sketchDataList.add(sketchData);
                mCurrentIndex = sketchDataList.size();
                break;
            case OPERATE_DEL:
                if (sketchData != null) {
                    sketchDataList.remove(sketchData);
                }
                break;
            case OPERATE_SEL:
                if (sketchData != null) {
                    int position = sketchDataList.indexOf(sketchData);
                    mCurrentIndex = position + 1;
                }
                break;
        }
        if (mCallback != null) {
            mCallback.onPageIndexRefresh();
        }
    }

    ControlManager.ControlCallback mCallback;

    public void setCallback(ControlManager.ControlCallback callback) {
        mCallback = callback;
    }

    public int getIndex(SketchData item) {
        return sketchDataList.indexOf(item);
    }

    public SketchData getPrevious() {
        int previousIndex = mCurrentIndex - 2;
        if (previousIndex < 0) {
            return null;
        }
        mCurrentIndex--;
        return sketchDataList.get(previousIndex);
    }

    public SketchData getNext() {
        int nextIndex = mCurrentIndex;
        if (nextIndex >= sketchDataList.size()) {
            return null;
        }
        mCurrentIndex++;
        return sketchDataList.get(nextIndex);
    }

    public SketchData get(int position) {
        return sketchDataList.get(position);
    }

    public void refreshCurrentState() {
        get(mCurrentIndex - 1).setSaved(true);
    }

    public SketchData getLatestSketchData() {
        if (sketchDataList.size() > 0) {
            return sketchDataList.get(sketchDataList.size() - 1);
        }
        return addNewSketchData();
    }
}
